package com.xyzwps.harbor.handler.demo;

import static com.xyzwps.harbor.util.HarborException.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.HandlerFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import java.util.Map;

@Component
public class DemoHandler {

    public Mono<ServerResponse> hello(ServerRequest req) {
        return ServerResponse.ok().bodyValue(Map.of("hello", req.queryParam("demo" ).orElse("world" )));
    }


    public Mono<ServerResponse> demoFilter(ServerRequest req, HandlerFunction<ServerResponse> next) {
        if (!req.queryParam("demo" ).orElse("no" ).equals("ok" )) {
            throw badRequestEx("不行" );
        }

        return next.handle(req).map(resp -> {
            log.info("通过了呢" );
            return resp;
        });

    }

    private static final Logger log = LoggerFactory.getLogger(DemoHandler.class);
}